package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wanghaotian
 * time:2022/12/21,0021 9:20
 * description:
 */

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    private DtsCategoryMapper categoryMapper;

    @Autowired
    private DtsGoodsMapper goodsMapper;

    @Override
    public Integer findL1ByL2(Integer categoryIdL2) {
        DtsCategory category = categoryMapper.selectById(categoryIdL2);
        return category.getPid();
    }

    @Override
    public List<CatVo> findCategoryList() {
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getLevel,"L1");
        wrapper.eq(DtsCategory::getDeleted,false);
        List<DtsCategory> categoryList = categoryMapper.selectList(wrapper);
        List<CatVo> list = new ArrayList<>();
        for (DtsCategory category : categoryList) {
            CatVo catVo = new CatVo();
            catVo.setValue(category.getId());
            catVo.setLabel(category.getName());
            LambdaQueryWrapper<DtsCategory> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DtsCategory::getLevel,"L2");
            wrapper1.eq(DtsCategory::getPid,category.getId());
            wrapper1.eq(DtsCategory::getDeleted,false);
            List<DtsCategory> categoryList1 = categoryMapper.selectList(wrapper1);
            List<CatVo> collect = categoryList1.stream().map((category1) -> {
                CatVo catVo1 = new CatVo();
                catVo1.setValue(category1.getId());
                catVo1.setLabel(category1.getName());
                return catVo1;
            }).collect(Collectors.toList());
            catVo.setChildren(collect);
            list.add(catVo);
        }
        return list;
    }

    @Override
    public IPage<DtsCategory> findPage(Integer page, Integer limit, String id, String name, String sort, String order) {
        IPage<DtsCategory> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", false);
        if (!StringUtils.isEmpty(id)) {
            wrapper.like("id", id);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("desc")) {
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        iPage = categoryMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public List<CatVo> findL1List() {
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getDeleted,false);
        wrapper.eq(DtsCategory::getPid,"0");
        List<DtsCategory> categoryList = categoryMapper.selectList(wrapper);
        List<CatVo> collect = categoryList.stream().map((category) -> {
            CatVo catVo = new CatVo();
            catVo.setValue(category.getId());
            catVo.setLabel(category.getName());
            return catVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public DtsCategory insert(DtsCategory category) {
        LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCategory::getName,category.getName());
        DtsCategory dtsCategory = categoryMapper.selectOne(wrapper);
        if (dtsCategory != null) {
            throw new RuntimeException("商品类目已存在");
        }
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public void update(DtsCategory category) {
        DtsCategory oldCategory = categoryMapper.selectById(category);

        if(!category.getName().equals(oldCategory.getName())){
            LambdaQueryWrapper<DtsCategory> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DtsCategory::getName,category.getName());
            DtsCategory category1 = categoryMapper.selectOne(wrapper);
            if(category1.getName()!=null){
                throw new RuntimeException("商品类目已存在");

            }
        }

        category.setUpdateTime(new Date());
        categoryMapper.updateById(category);
    }

    @Override
    public void delete(DtsCategory category) {
        LambdaQueryWrapper<DtsGoods> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DtsGoods::getCategoryId, category.getId());
        DtsGoods goods = goodsMapper.selectOne(wrapper1);
        if (goods != null) {
            throw new RuntimeException("该类目正在使用不能删除");
        }
        if (category.getPid() == 0) {
            QueryWrapper<DtsCategory> wrapper = new QueryWrapper<>();
            wrapper.eq("pid", category.getId());
            wrapper.eq("deleted", false);
            List<DtsCategory> list = categoryMapper.selectList(wrapper);
            if (list.size() > 0) {
                throw new RuntimeException("存在二级类目，不能删除");
            }
        }
        categoryMapper.deleteById(category.getId());
    }
}
